﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using BrokerBase.Lib.Policy;
using BrokerBase.Lib.Person;
using System.ComponentModel;
using Broker.View.ViewModel;
using Telerik.Windows.Controls;
using BrokerBase.Lib.Core;
using NHibernate;
using Broker.View.PersonView;

namespace Broker.View.PolicyView
{
    /// <summary>
    /// NormalSchemeHead.xaml 的交互逻辑
    /// </summary>
    public partial class NormalSchemeHead :SchemeHeadUserControl
    {
        private IPerson insurant;
        private Scheme2Form schemeForm;

        private BackgroundWorker worker = new BackgroundWorker();

        private IList<PersonNameModel> personModels;

        public NormalSchemeHead(IScheme scheme):base(scheme)
        {
            InitializeComponent();

            this.Loaded += new RoutedEventHandler(NormalSchemeHead_Loaded);

            worker.DoWork += new DoWorkEventHandler(worker_DoWork);
            worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);
        }



        public NormalSchemeHead(IScheme scheme, IPerson insurant,Scheme2Form schemeForm)
            : this(scheme)
        {
            this.insurant = insurant;
            this.schemeForm = schemeForm;
        }

        void NormalSchemeHead_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                DataContext = Scheme;

                cmbInsurant.IsEnabled = !Scheme.IsCheck;
                cmbHolder.IsEnabled = !Scheme.IsCheck;
                cmbBeneficiary.IsEnabled = !Scheme.IsCheck;

                dpBillDate.IsEnabled = !Scheme.IsCheck;

                if (!worker.IsBusy)
                {
                    buyRoot.IsBusy = true;
                    worker.RunWorkerAsync();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }

        void worker_DoWork(object sender, DoWorkEventArgs e)
        {
            
            personModels = PersonNameModel.LoadAllPerson();
        }

        void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            cmbInsurant.ItemsSource = personModels;
            cmbHolder.ItemsSource = personModels.Where(p => p.Age >= 18);
            cmbBeneficiary.ItemsSource = personModels;

            if (Scheme.Insurant != null)
            {
                IPKItem insurant = Scheme.Insurant as IPKItem;
                cmbInsurant.SelectedItem = personModels.FirstOrDefault(p => p.Id == insurant.Id);
            }

            if (Scheme.Holder != null)
            {
                IPKItem holder = Scheme.Holder as IPKItem;
                cmbHolder.SelectedItem = personModels.FirstOrDefault(p => p.Id == holder.Id);
            }

            if (Scheme.Beneficiary != null)
            {
                IPKItem bfy = Scheme.Beneficiary as IPKItem;
                cmbBeneficiary.SelectedItem = personModels.FirstOrDefault(p => p.Id == bfy.Id);
            }

            buyRoot.IsBusy = false;
        }

        private void btnHolderSel_Click(object sender, RoutedEventArgs e)
        {
            PersonView.PersonListPage plp = new PersonView.PersonListPage();

            try
            {
                IPerson p = plp.SelectByHQL(null, this.Owner, 400, 600);

                if (p != null)
                {
                    if ((p.Age != Scheme.HolderAge || p.Sex != Scheme.HolderSex) && Scheme.Policies.Count > 0)
                    {
                        MessageBox.Show("新的投保人与原来的投保人性别或年龄不符合!", "提示");
                    }
                    else
                    {
                        this.Scheme.Holder = p;
                        this.Scheme.HolderAge = p.Age;
                        this.Scheme.HolderSex = p.Sex;
                        //epdHolder.DataContext = null;
                        //epdHolder.DataContext = Scheme;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "错误提示");
            }
        }

        private void btnInsurantSel_Click(object sender, RoutedEventArgs e)
        {
            PersonView.PersonListPage plp = new PersonView.PersonListPage();

            IPerson p = plp.SelectByHQL(null, this.Owner, 400, 600);

            if (p != null)
            {
                if ((p.Age != Scheme.InsurantAge || p.Sex != Scheme.InsurantSex) && Scheme.Policies.Count > 0)
                {
                    MessageBox.Show("新的被保险人与原来的被保险人性别或年龄不符合!", "提示");
                }
                else
                {
                    this.Scheme.Insurant = p;
                    this.Scheme.InsurantSex = p.Sex;
                    this.Scheme.InsurantAge = p.Age;
                    //epdInsurant.DataContext = null;
                    //epdInsurant.DataContext = Scheme;
                }
            }
        }

        private void btnBeneficiarySel_Click(object sender, RoutedEventArgs e)
        {
            PersonView.PersonListPage plp = new PersonView.PersonListPage();

            IPerson p = plp.SelectByHQL(null, this.Owner, 400, 600);

            if (p != null)
            {
                this.Scheme.Beneficiary = p;
                //epdBeneficiary.DataContext = null;
                //epdBeneficiary.DataContext = Scheme;
            }
        }

        public override void SetControlState(IScheme scheme)
        {
            cmbInsurant.IsEnabled = !Scheme.IsCheck;
            cmbHolder.IsEnabled = !Scheme.IsCheck;
            cmbBeneficiary.IsEnabled = !Scheme.IsCheck;

            dpBillDate.IsEnabled = !Scheme.IsCheck;

            dpCheckDate.SelectedDate = scheme.CheckTime;
            dpCheckDate.IsEnabled = !scheme.IsCheck;
        }

        private void cmb_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            RadComboBox cmb = sender as RadComboBox;

            cmb.IsDropDownOpen = true;
        }

        private void cmb_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            RadComboBox cmb=sender as RadComboBox;
            PersonNameModel pm = cmb.SelectedItem as PersonNameModel;

            if (pm != null)
            {
                using (ISession sess = LibGlobal.HbmSessionFactory.OpenSession())
                {
                    IPerson person = sess.Get<Person>(pm.Id);

                    if (cmb.Name == "cmbInsurant")
                    {
                        Scheme.Insurant = person;
                        Scheme.InsurantAge = person.Age;
                        Scheme.InsurantSex = person.Sex;

                    }
                    else if (cmb.Name == "cmbHolder")
                    {
                        Scheme.Holder = person;
                        Scheme.HolderAge = person.Age;
                        Scheme.HolderSex = person.Sex;
                    }
                    else if (cmb.Name == "cmbBeneficiary")
                    {
                        Scheme.Beneficiary = person;
                    }
                }
            }
        }

        private void btnAddPerson_Click(object sender, RoutedEventArgs e)
        {
            IPerson person = new Person();

            person.Birthday = LibGlobal.GetClientCfg().DefaultBirthday;
            person.Sex = SexType.Male;
            person.Attention = 3;
            PersonForm frm = new PersonForm(person, ReQuery);

            frm.WindowStartupLocation = WindowStartupLocation.CenterOwner;
            frm.Owner = LibGlobal.MainWin;
            frm.ShowDialog();
        }

        private void ReQuery()
        {
            try
            {
                buyRoot.IsBusy = true;
                worker.RunWorkerAsync();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                buyRoot.IsBusy = false;
            }
        }

    }
}
